#include <iostream>
using namespace std;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
class Solution {
public:
    bool _isSameTree(TreeNode* p, TreeNode* q)
    {
        if (p == nullptr && q == nullptr)
        {
            return true;
        }
        if (p == nullptr || q == nullptr)
        {
            return false;
        }
        if (p->val != q->val)
        {
            return false;
        }

        return _isSameTree(p->left, q->left) &&
            _isSameTree(p->right, q->right);
    }
    bool _isSubtree(TreeNode* root, TreeNode* subRoot)
    {
        if (_isSameTree(root, subRoot))  return true;
        if (root == nullptr)  return false;
        return _isSubtree(root->left, subRoot)
            || _isSubtree(root->right, subRoot);
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        bool ret = _isSubtree(root, subRoot);
        return ret;
    }
};